package org.jeecg.modules.supplier.controller;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.supplier.dto.ProductRelationDto;
import org.jeecg.modules.supplier.service.PdProductService;
import org.jeecg.modules.supplier.service.PdPurchaseDetailService;
import org.jeecg.modules.supplier.service.impl.PdPurchaseDetailServiceImpl;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 申购订单明细表 前端控制器
 * </p>
 *
 * @author twm
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/pd-purchase-detail")
public class PdPurchaseDetailController extends BaseController{


    @Autowired
    public PdPurchaseDetailService pdPurchaseDetailService;
    @Autowired
    public PdProductService productService;
    /**
     *
     * 根据不同的供应商查询相关的产品
     * @return
     */
    @GetMapping("get_produce_relation")
    private Result getProduceRelation(@RequestParam(value = "startDate",required = false) String startDate,
                                      @RequestParam(value = "endDate",required = false) String endDate,
                                      @RequestParam(value = "status",required = false) Integer status,
                                      @RequestParam(value = "spec",required = false) String spec,
                                      @RequestParam(value = "name",required = false) String name,
                                      @RequestParam(value = "dis_start_date",required = false) String disStartDate,
                                      @RequestParam(value = "dis_end_date",required = false) String disEndDate) {

        PdPurchaseDetailService bean = SpringContextUtils.getBean(PdPurchaseDetailService.class);
        //根据session 获取供应商id
        String supplier = "";

        SysUser currentUser = getCurrentUser();
        if(Objects.isNull(currentUser)) {
            return Result.error("错误");
        }
        supplier = currentUser.getSocialCreditCode();

        if(StringUtils.isEmpty(supplier)) {
           return Result.error("供应商编号不能为空");
        }
        List<ProductRelationDto> produceRelation = bean.getProduceRelation(supplier, startDate, endDate, status,spec,
                name,disStartDate,disEndDate);
        return Result.ok(produceRelation);

    }

    /**
     * 修改订单状态
     * @Author twm
     * @Date 2021/6/26 13:53
     * @Param
     * @return org.jeecg.common.api.vo.Result
     **/
    @GetMapping("updata_order_form")
    private Result updataOrderFormStatus(@RequestParam("orderFormId") String orderFormId,
                                         @RequestParam("status") Integer status) {
        PdPurchaseDetailService bean = SpringContextUtils.getBean(PdPurchaseDetailService.class);
        if(bean.updataOrderFormStatus(orderFormId,status)) {
            return Result.ok();
        }else {
            return Result.error("修改失败");
        }
    }

}

